package main;

import com.github.crab2died.ExcelUtils;
import com.github.crab2died.annotation.ExcelField;
import model.statical.Model;
import model.dynamic.DyDepartment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GenerateRandom
{
	public static void main(String[] args)
	{
		try
		{
			List<Model> shiyouList = ExcelUtils.getInstance()
					.readExcel2Objects("附件.xlsx", Model.class, 1);
			List<RandomModel> randomModels = new ArrayList<>();
			for (Model model : shiyouList)
			{
				for (int i = 0; i < model.getNumber(); i++)
					randomModels.add(new RandomModel(model));
			}
			RandomModel.generateRandomDatas();
			ExcelUtils.getInstance().exportObjects2Excel(randomModels,
					RandomModel.class, "随机数据.xlsx");

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static class RandomModel
	{
		// 等级提升比例
		static final double[] promoteRates = DyDepartment.promoteRates;
		// 等级最低服役年限
		static final int[] rankLeastTimes = DyDepartment.rankLeastTimes;
		// 等级最高服役年限
		static final int[] rankLimitTimes = DyDepartment.rankLimitTimes;
		// 等级最高服役年龄
		static final int[] rankLimitAges = DyDepartment.rankLimitAges;
		// 等级参考最低年龄
		static final int[] rankLowReferAges = DyDepartment.rankLowReferAges;

		public final static ArrayList<RandomModel>[] randomModelLists;

		static
		{
			randomModelLists = new ArrayList[rankLimitTimes.length];
			for (int i = 0; i < randomModelLists.length; i++)
				randomModelLists[i] = new ArrayList<>();
		}

		public static void generateRandomDatas()
		{
			for (ArrayList<RandomModel> randomModelList : randomModelLists)
			{
				Random random1 = new Random();
				Random random2 = new Random();
				for (RandomModel randomModel : randomModelList)
					randomModel.generateRandomData(random1, random2);
			}
		}

		static int getGaussian(Random random, int range)
		{
			double half = range / 2.0;
			int g = (int) (random.nextGaussian() / 2 * half + half);
			return g < 0 ? 0 : Math.min(g, range);
		}

		public RandomModel()
		{
		}

		public RandomModel(Model model)
		{
			this.aName = model.getaName();
			this.bName = model.getbName();
			this.cName = model.getcName();
			this.dName = model.getdName();
			this.quarter = model.getQuarter();
			this.rankString = model.getRankString();
			this.profession = model.getProfession();
			this.rank = model.getRank();
			randomModelLists[rank - 1].add(this);
		}

		@ExcelField(title = "A级单位", order = 1)
		String aName;

		@ExcelField(title = "B级单位", order = 2)
		String bName;

		@ExcelField(title = "C级单位", order = 3)
		String cName;

		@ExcelField(title = "D级单位", order = 4)
		String dName;

		@ExcelField(title = "岗位", order = 5)
		String quarter;

		@ExcelField(title = "等级", order = 6)
		String rankString;

		@ExcelField(title = "专业", order = 7)
		String profession;

		// 等级
		int rank;
		// 等级服役时间
		@ExcelField(title = "等级服役时间", order = 8)
		int rankServiceTime;
		// 服役年龄
		@ExcelField(title = "年龄", order = 9)
		int serviceAge;

		void generateRandomData(Random random1, Random random2)
		{
			int index = rank - 1;
			int range = rankLimitTimes[index] - 1;
			rankServiceTime = getGaussian(random1, range);
			int lowAge = rankLowReferAges[index] + rankServiceTime;
			int limitAge = rankLimitAges[index];
			if (index > 0)
			{
				limitAge = rankLimitAges[index - 1] + rankServiceTime - 1;
			}
			range = limitAge - lowAge;
			serviceAge = getGaussian(random2, range) + lowAge;
			rankServiceTime++;
		}

		public String getaName()
		{
			return aName;
		}

		public void setaName(String aName)
		{
			this.aName = aName;
		}

		public String getbName()
		{
			return bName;
		}

		public void setbName(String bName)
		{
			this.bName = bName;
		}

		public String getcName()
		{
			return cName;
		}

		public void setcName(String cName)
		{
			this.cName = cName;
		}

		public String getdName()
		{
			return dName;
		}

		public void setdName(String dName)
		{
			this.dName = dName;
		}

		public String getQuarter()
		{
			return quarter;
		}

		public void setQuarter(String quarter)
		{
			this.quarter = quarter;
		}

		public String getRankString()
		{
			return rankString;
		}

		public void setRankString(String rankString)
		{
			this.rankString = rankString;
			rank = Integer.parseInt(rankString.substring(1));
		}

		public String getProfession()
		{
			return profession;
		}

		public void setProfession(String profession)
		{
			this.profession = profession;
		}

		public int getRankServiceTime()
		{
			return rankServiceTime;
		}

		public void setRankServiceTime(int rankServiceTime)
		{
			this.rankServiceTime = rankServiceTime;
		}

		public int getServiceAge()
		{
			return serviceAge;
		}

		public void setServiceAge(int serviceAge)
		{
			this.serviceAge = serviceAge;
		}

		public int getRank()
		{
			return rank;
		}

		public void setRank(int rank)
		{
			this.rank = rank;
		}
	}
}
